<?php
class XBootConf
{
    private $_layout;
    private $_volume;
	private $_volume_percent;
    private $_screen_switch;
    private $_play_timeout;
	private $_discount_x;
	private $_discount_y;
	private $_discount_width;
	private $_discount_heigh;
	private $_is_discount;
	
	private $_boot_conf;
	
	function __construct()
	{
		$this->readBootConf();
	}

	public function getScreenSwitch() { return $this->_screen_switch; }
	public function getLayout() { return $this->_layout;}
	public function getVolumePercent() { return $this->_volume_percent;}
	public function getTimeout() {return $this->_play_timeout/1000;}
	public function isSetDiscount() { return $this->_is_discount;}
	
	public function writeLayout($layout)
	{
		$this->_layout = $layout;
		$filename = '/usr/infomat/boot.conf';
		$data = parse_ini_file($filename);
		$replace_with = array(
  			'Boot_Layout' => $this->_layout
		);
		$fh = fopen($filename, 'w');
		foreach ( $data as $key => $value )
		{
  			if ( ! empty($replace_with[$key]) )
    			$value = $replace_with[$key];
  			fwrite($fh, "{$key}={$value}" . PHP_EOL);
		}
		fclose($fh);
	}
	
	public function writeVolume($volumePercent)
	{
		$filename = '/usr/infomat/boot.conf';
		$data = parse_ini_file($filename);
		$this->_volume_percent =  $volumePercent;
		$this->_volume = $volumePercent / 100 * 50 + 60;
		$replace_with = array(
  			'volume' => $this->_volume
		);
		$fh = fopen($filename, 'w');
		foreach ( $data as $key => $value )
		{
  			if ( ! empty($replace_with[$key]) )
    			$value = $replace_with[$key];
  			fwrite($fh, "{$key}={$value}" . PHP_EOL);
		}
		fclose($fh);
	}
	
	public function writeScreenSwitch($isTick)
	{
		$this->_screen_switch = $isTick;
		$filename = '/usr/infomat/boot.conf';
		$data = parse_ini_file($filename);
		$replace_with = array(
  			'ScreenSwitch' => $this->_screen_switch
		);
		$fh = fopen($filename, 'w');
		foreach ( $data as $key => $value )
		{
  			if ( ! empty($replace_with[$key]) )
    			$value = $replace_with[$key];
  			fwrite($fh, "{$key}={$value}" . PHP_EOL);
		}
		fclose($fh);
	}
	
	public function writeTimeout($timeout)
	{
		$filename = '/usr/infomat/boot.conf';
		$data = parse_ini_file($filename);
		$this->_play_timeout = $timeout * 1000;
		$replace_with = array(
  			'Timeout' => $this->_play_timeout
		);
		$fh = fopen($filename, 'w');
		foreach ( $data as $key => $value )
		{
  			if ( ! empty($replace_with[$key]) )
    			$value = $replace_with[$key];
  			fwrite($fh, "{$key}={$value}" . PHP_EOL);
		}
		fclose($fh);
	}
	
	public function writeDiscountMessage($isSet)
	{
		$this->_is_discount = $isSet;
		$this->writeDiscountWidth();
		$this->writeDiscountHeigh();
	}
	
	private function writeDiscountWidth()
	{
		$filename = '/usr/infomat/boot.conf';
		$data = parse_ini_file($filename);
		if ($this->_is_discount == 1) {
			$width = 225; 
		} else {
			$width = 1;
		}
				
		$replace_with = array(
  			'DiscountWidth' => $width
		);

		$fh = fopen($filename, 'w');
		foreach ( $data as $key => $value )
		{
  			if ( ! empty($replace_with[$key]) )
    			$value = $replace_with[$key];
  			fwrite($fh, "{$key}={$value}" . PHP_EOL);
		}
		fclose($fh);
	}
	
	private function writeDiscountHeigh()
	{
		$filename = '/usr/infomat/boot.conf';
		$data = parse_ini_file($filename);
		if ($this->_is_discount == 1) {
			$heigh = 50;
		} else {
			$heigh = 1;
		}
		$replace_with = array(
  			'DiscountHeigh' => $heigh
		);
		$fh = fopen($filename, 'w');
		foreach ( $data as $key => $value )
		{
  			if ( ! empty($replace_with[$key]) )
    			$value = $replace_with[$key];
  			fwrite($fh, "{$key}={$value}" . PHP_EOL);
		}
		fclose($fh);
	}
	
    public function readBootConf()
    {
        $file = "/usr/infomat/boot.conf";
        if (false === ($fd = fopen($file, 'r'))) {
			die("[NG] Open boot.conf\r");
            $this->_layout = $this->_volume = $this->_screen_switch = $this->_play_timeout = "[NG] Cannot Find Configure File";
            return;
		}
        
        $tmpVolume = trim(fgets($fd)); //line1
		$this->_volume = str_replace("volume=","", $tmpVolume); 
		$this->VolumeConvert();
        fgets($fd); //line2
        fgets($fd); //line3
        $tmpLayout = trim(fgets($fd)); //line4
		$this->_layout = str_replace("Boot_Layout=","", $tmpLayout);
        fgets($fd);     //line5
        fgets($fd);     //line6
        fgets($fd);     //line7
        fgets($fd);     //line8
        fgets($fd);     //line9
        fgets($fd);     //line10
        fgets($fd);     //line11
        $tmpTimeout = trim(fgets($fd)); //line12
		$this->_play_timeout = str_replace("Timeout=","", $tmpTimeout);       
        fgets($fd);     //line13
        fgets($fd);     //line14
		
        $tmpScreenMode = trim(fgets($fd)); //line15
		$this->_screen_switch = str_replace("ScreenMode=","", $tmpScreenMode);
		
		fgets($fd);     //line16
		fgets($fd);     //line17
		$tmpDiscountX = trim(fgets($fd));  //line 18 
		$this->_discount_x = str_replace("DiscountStartX=","", $tmpDiscountX);
		//echo $this->_discount_x;
		
		$tmpDiscountY = trim(fgets($fd));  // Line 19
		$this->_discount_y = str_replace("DiscountStartY=","", $tmpDiscountY);
		//echo $this->_discount_y;

		$tmpDiscountWidth = trim(fgets($fd)); //Line 20
		$this->_discount_width = str_replace("DiscountWidth=","", $tmpDiscountWidth);
		//echo $this->_discount_width;
		
		$tmpDiscountHeigh = trim(fgets($fd)); //Line 21
		$this->_discount_heigh = str_replace("DiscountHeigh=","", $tmpDiscountHeigh);
		//echo $this->_discount_heigh;
		
		if ($this->_discount_x == 573 && $this->_discount_y == 5 &&
		    $this->_discount_width == 225 && $this->_discount_heigh == 50 ) {
			$this->_is_discount = 1;
		}else {
			$this->_is_discount = 0;
		}
        fclose($fd);
    }
    
	private function VolumeConvert()
	{
		$this->_volume_percent = ($this->_volume - 60) / 50 * 100;
	}    
}

//$bootconf = new XBootConf();
//$timeout = $bootconf->getTimeout();
//$bootconf->writeVolume(100);
//$bootconf->writeDiscountMessage();
?>